package com.hutiao.snowhb.service;

import java.sql.SQLException;
import java.util.Date;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.hutiao.snowhb.common.DateTimeUtil;
import com.hutiao.snowhb.dao.BaseDao;
import com.hutiao.snowhb.entities.SnowMMLocation;
import com.hutiao.snowhb.entities.SnowMMPrebook;

@Component
public class SnowPrebookService {

	private static Logger logger = Logger.getLogger(SnowPrebookService.class);
	
	@Autowired
	private BaseDao baseDao;
	
	
	public int savePrebook(SnowMMPrebook spb) throws SQLException {
		
		String sql = "";
		if (spb.getArriTime() != null && !spb.getArriTime().equals("")) {
			//arriTime = DateTimeUtil.Date2String(DateTimeUtil.String2Date(spb.get_arriTime(), "yyyy-MM-dd HH:mm"));
			sql = "insert into snowmm_prebook(openId,mmid,mlid,custNum,custName,custPhone,arriTime,status,createTime,updateTime) "
					+ "values('"+spb.getOpenId()+"',"+spb.getMmid()+","+spb.getMlid()+","+spb.getCustNum()+",'"+spb.getCustName()+"','"+spb.getCustPhone()+"','"+spb.getArriTime()+"',0,now(),now());";
		} else {
			sql = "insert into snowmm_prebook(openId,mmid,mlid,custNum,custName,custPhone,status,createTime,updateTime) "
					+ "values('"+spb.getOpenId()+"',"+spb.getMmid()+","+spb.getMlid()+","+spb.getCustNum()+",'"+spb.getCustName()+"','"+spb.getCustPhone()+"',0,now(),now());";
		}
		
		return this.baseDao.save(sql);
	}
	
	/**
	 * 0-预订中 1-预订成功 2-预订失败  3-取消预订 4-过期
	 * @param id
	 * @param status
	 * @param msg
	 */
	public void updatePrebook(int id, int status, String msg) {
		String sql = "update snowmm_prebook set status="+status+",updateTime=now(),message='"+msg+"' where id="+id+"";
		
		this.baseDao.execute(sql);
	}
}
